﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
using System.Xml;
using System.Runtime.Serialization;

namespace ClientApplication
{
    [DataContract]
    [Serializable]
    public class Result
    {
        [DataMember]
        public Rules rules { get; private set; }
        [DataMember]
        public ContestantList allContestants { get; private set; }
        [DataMember]
        public List<Judge> allJudges { get; private set; }

        public Result()
        {
            this.rules = new Rules();
            this.allContestants = new ContestantList();
            this.allJudges = new List<Judge>();
        }

        public Result(Rules rules, ContestantList allContestants, List<Judge> allJudges)
        {
            this.rules = rules;
            this.allContestants = allContestants;
            this.allJudges = allJudges;
        }
        //Saves the result as an XML file = @"c:\Result.xml"
        public void SaveResult(string filename)
        {
            FileStream writer = new FileStream(filename, FileMode.Create);

            DataContractSerializer ser = new DataContractSerializer(typeof(Result));

            ser.WriteObject(writer, this);
            writer.Close();
        }
        // = @"c:\Result.xml"
        public Result LoadResult(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open);

            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());

            DataContractSerializer ser = new DataContractSerializer(typeof(Result));

            // Deserialize the data and read it from the instance.
            Result r =
                (Result)ser.ReadObject(reader, true);

            reader.Close();
            fs.Close();

            return r;
        }

        public void PresentResult()
        {
            for (int i = 0; i < 3; i++)//Lägg till kompabilitet för olika antal hopp
            {
                for (int f = 0; f < allContestants.contestants.Count; f++)
                {
                    Console.WriteLine("Tävlande " + allContestants[f].name + "s hopp nummer " + (i + 1) + ":");
                    for (int j = 0; j < allContestants[f].jumps[i].judgesScore.Count(); j++)
                    {
                        Console.WriteLine("Domare " + allContestants[f].jumps[i].judgesScore[j].id +  " gav hoppet " + allContestants[f].jumps[i].judgesScore[j].score + " poäng.");
                    }
                    Console.WriteLine("Total poäng: " + allContestants[f].jumps[i].totalScore + " poäng.");
                    Console.WriteLine("");
                }
                Console.WriteLine("");
            }
            allContestants.PrintList();
        }
    }
}